<?php
namespace Home\Controller;

class TeamController extends FuncController {

    function __construct(){
        parent::__construct();

        $this->mdlTeam = D('team');
        $this->mdlPlayer = D('player');
        $this->mdlTeamPlayer = D('team_player');
        $this->mdlMsg = D('msg');

        $this->player = $this->mdlPlayer->findPlayer($this->userid);
    }

    public function index(){
        // list
        $teamList = $this->mdlTeam->findTeamByPlayerId($this->player['player_id']);
        $this->assign('teamList',$teamList);

        $this->pDisplay( T('Team/index'), true );
    }

    public function create(){

        $this->player['user_id'] = $this->userid;
        $this->assign('player',$this->player);

        $this->pDisplay( T('Team/create'), true );
    }

    public function createTeam(){
        // save team
        $rs = $this->mdlTeam->createTeam();
        if (!$rs['status']){
            $this->error($rs['msg'], '', 2);
            exit;
        }
        // save team player
        $dt = array(
            'team_id' => $rs['dt']['team_id'],
            'player_id' => $this->player['player_id'],
        );
        $rs = $this->mdlTeamPlayer->createTeamPlayer($dt, true);

        if (!$rs['status']){
            $this->error($rs['msg'], '', 2);
        } else {
            $this->success($rs['msg'], './index.php?m=Home&c=Team&a=index', 2);
        }
    }

    public function edit(){
        $team = $this->mdlTeam->findTeam($_GET['team_id']);
        $team[0]['player_id'] = $this->player['player_id'];

        $this->assign('team',$team[0]);
        $this->pDisplay( T('Team/create'), true );
    }

    public function left(){
        // save team player
        $dt = array(
            'team_id' => $_GET['team_id'],
            'player_id' => $this->player['player_id'],
        );
        $rs = $this->mdlTeamPlayer->left($dt);

        if (!$rs['status']){
            $this->error($rs['msg'], '', 2);
        } else {
            $this->success($rs['msg'], './index.php?m=Home&c=Team&a=index', 2);
        }
    }

    public function joinList(){
        // list
        $teamList = $this->mdlTeam->findTeam();
        $this->assign('teamList',$teamList);


        $this->pDisplay( T('Team/join'), true );
    }

    public function join(){
        // save team player
        $dt = array(
            'team_id' => $_GET['team_id'],
            'player_id' => $this->player['player_id'],
        );
        // $rs = $this->mdlTeamPlayer->createTeamPlayer($dt);
        $rs = $this->mdlTeamPlayer->join($dt);

        if (!$rs['status']){
            $this->error($rs['msg'], '', 2);
        } else {
            $this->success($rs['msg'], './index.php?m=Home&c=Team&a=index', 2);
        }
    }

    public function mangList(){
        $chargeList = $this->mdlTeamPlayer->chargeList($this->player['player_id']);
        $teamList = $this->mdlTeam->findTeams($chargeList);

        $this->assign('teamList',$teamList);
        $this->pDisplay( T('Team/mangList'), true );
    }

    public function mangTeam(){
        $teamId = $_GET['team_id'];
        // players
        $playerIds = $this->mdlTeamPlayer->findPlayerByTeamId($teamId);
        $players = $this->mdlPlayer->findPlayers($playerIds);

        // msg
        $msg = $this->mdlMsg->findLast($teamId);

        // calender


// $this->EE($msg);

        $this->assign('teamId',$teamId);
        $this->assign('msg',$msg);
        $this->assign('players',$players);

        $this->pDisplay( T('Team/mangTeam'), true );
    }

}
